package com.puman.beautyfarmcounselor.ui.home;


import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.ui.core.BaseFragment;
import com.puman.beautyfarmcounselor.ui.task.adpter.MemberListAdapter;
import com.puman.beautyfarmcounselor.ui.task.model.ChildTask;
import com.puman.beautyfarmcounselor.ui.task.model.GroupTask;
import com.puman.beautyfarmcounselor.ui.task.view.ExpendListViewCompat;
import com.puman.beautyfarmcounselor.ui.task.view.SlideView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;


/**
 * A placeholder fragment containing a simple view.
 */
@EFragment(R.layout.member_noticy_list_layout)
public class MemberNoticyFragment extends BaseFragment implements SlideView.OnSlideListener {

    /**
     * init UI
     */
    @ViewById(R.id.back_btn)
    ImageView backBtn;
    @ViewById(R.id.base_title)
    TextView title;
    @ViewById(R.id.list)
    ExpendListViewCompat mListView;
    /**
     * init variables
     */
    @FragmentArg
    String titleStr;

    private List<GroupTask> groupTaskList = new ArrayList<>();
    private MemberListAdapter slideAdapter;

    @AfterViews
    void updateView() {
        title.setText(titleStr);
        backBtn.setVisibility(View.VISIBLE);
        initData();


        slideAdapter = new MemberListAdapter(getActivity().getApplicationContext(),
                groupTaskList, this);
        slideAdapter.updateTreeNode(groupTaskList);
        mListView.setAdapter(slideAdapter);
        mListView.setGroupIndicator(null);
        for (int i = 0; i < slideAdapter.getGroupCount(); i++) {
            mListView.expandGroup(i);
        }

    }

    @Click({R.id.back_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstace().getSupportFragmentManager().popBackStack();
                break;
        }
    }


    private String[] groupName = {"3天回复", "7天跟进", "12天回访", "30天顾问"};
    private String[] childName = {"江新月", "张丹妮", "翁玉霞", "唐幕欣", "钟晓", "Elva",};

    private void initData() {

        for (int i = 0; i < 4; i++) {
            GroupTask groupTask = new GroupTask();
            groupTask.setName(groupName[i]);
            List<ChildTask> childList = new ArrayList<>();
            for (int j = 0; j < 6 + i * 2; j++) {
                ChildTask childTask = new ChildTask();
                childTask.setName(childName[j%(6 + i * 2)%6]);
                childList.add(childTask);
            }
            groupTask.setChildTaskList(childList);
            groupTaskList.add(groupTask);
        }

    }
    private SlideView mLastSlideViewWithStatusOn;

    @Override
    public void onSlide(View view, int status) {

        Log.i("curry", "onSlide listener....");
        if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
            mLastSlideViewWithStatusOn.shrink();
        }

        if (status == SLIDE_STATUS_ON) {
            mLastSlideViewWithStatusOn = (SlideView) view;
        }
    }

//    WeakHashMap<View,Integer> mOriginalViewHeightPool = new WeakHashMap<View, Integer>();
//
//    //animation executor
//    class AnimationExecutor implements ExpandableStickyListHeadersListView.IAnimationExecutor {
//
//        @Override
//        public void executeAnim(final View target, final int animType) {
//            if(ExpandableStickyListHeadersListView.ANIMATION_EXPAND==animType&&target.getVisibility()==View.VISIBLE){
//                return;
//            }
//            if(ExpandableStickyListHeadersListView.ANIMATION_COLLAPSE==animType&&target.getVisibility()!=View.VISIBLE){
//                return;
//            }
//            if(mOriginalViewHeightPool.get(target)==null){
//                mOriginalViewHeightPool.put(target,target.getHeight());
//            }
//            final int viewHeight = mOriginalViewHeightPool.get(target);
//            float animStartY = animType == ExpandableStickyListHeadersListView.ANIMATION_EXPAND ? 0f : viewHeight;
//            float animEndY = animType == ExpandableStickyListHeadersListView.ANIMATION_EXPAND ? viewHeight : 0f;
//            final ViewGroup.LayoutParams lp = target.getLayoutParams();
//            ValueAnimator animator = ValueAnimator.ofFloat(animStartY, animEndY);
//            animator.setDuration(200);
//            target.setVisibility(View.VISIBLE);
//            animator.addListener(new Animator.AnimatorListener() {
//                @Override
//                public void onAnimationStart(Animator animator) {
//                }
//
//                @Override
//                public void onAnimationEnd(Animator animator) {
//                    if (animType == ExpandableStickyListHeadersListView.ANIMATION_EXPAND) {
//                        target.setVisibility(View.VISIBLE);
//                    } else {
//                        target.setVisibility(View.GONE);
//                    }
//                    target.getLayoutParams().height = viewHeight;
//                }
//
//                @Override
//                public void onAnimationCancel(Animator animator) {
//
//                }
//
//                @Override
//                public void onAnimationRepeat(Animator animator) {
//
//                }
//            });
//            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                @Override
//                public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                    lp.height = ((Float) valueAnimator.getAnimatedValue()).intValue();
//                    target.setLayoutParams(lp);
//                    target.requestLayout();
//                }
//            });
//            animator.start();
//
//        }
//    }
}
